package com.dcjt.firehotcarpad.helper;

import android.content.Context;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.allen.library.SuperTextView;
import com.aries.library.fast.util.SPUtil;
import com.aries.ui.view.radius.RadiusCheckBox;
import com.dcjt.firehotcarpad.R;
import com.dcjt.firehotcarpad.base.SPConstant;
import com.zyyoona7.popup.EasyPopup;
import com.zyyoona7.popup.XGravity;
import com.zyyoona7.popup.YGravity;

/**
 * Created by cj on 2021/3/1.
 * Email:codesexy@163.com
 * Function:
 * desc:
 */
public class NavTypeSelectHelper {


    public static void showChoosePanel(Fragment fragment, TextView textView, PopupWindow.OnDismissListener listener) {
        Context context = fragment.getContext();
        EasyPopup easyPopup = EasyPopup.create(context).setContentView(R.layout.layout_pop_nav_type)
                .setAnimationStyle(R.style.navTypePopAnim)
                .setFocusAndOutsideEnable(false)
                .setWidth(fragment.getView().getWidth() * 4 / 10)
                .setBackgroundDimEnable(true)
                .setDimValue(0.4f)
                .apply();

        easyPopup.showAtAnchorView(textView, YGravity.ALIGN_BOTTOM, XGravity.ALIGN_LEFT, textView.getWidth(), 0);

        View contentView = easyPopup.getContentView();
        SuperTextView stvClose = contentView.findViewById(R.id.stvClose);
        RadiusCheckBox smartRecom = contentView.findViewById(R.id.rcbSmartRecom);
        RadiusCheckBox noHighSpeed = contentView.findViewById(R.id.rcbNoHighSpeed);
        RadiusCheckBox avoidCharges = contentView.findViewById(R.id.rcbAvoidCharges);
        RadiusCheckBox avoidCongestion = contentView.findViewById(R.id.rcbAvoidCongestion);

        boolean smart = (boolean) SPUtil.get(context, SPConstant.TAG_SMART_RECOM, true);
        boolean noHigh = (boolean) SPUtil.get(context, SPConstant.TAG_NO_HIGH_SPEED, false);
        boolean avoidCharge = (boolean) SPUtil.get(context, SPConstant.TAG_AVOID_CHARGES, false);
        boolean avoidCongest = (boolean) SPUtil.get(context, SPConstant.TAG_AVOID_CONGESTION, false);

        smartRecom.setChecked(smart);
        noHighSpeed.setChecked(noHigh);
        avoidCharges.setChecked(avoidCharge);
        avoidCongestion.setChecked(avoidCongest);

        stvClose.getRightTextView().setOnClickListener(v -> {
            easyPopup.dismiss();
            if ((boolean) SPUtil.get(context, SPConstant.TAG_SMART_RECOM, true) != smart ||
                    (boolean) SPUtil.get(context, SPConstant.TAG_NO_HIGH_SPEED, false) != noHigh ||
                    (boolean) SPUtil.get(context, SPConstant.TAG_AVOID_CHARGES, false) != avoidCharge ||
                    (boolean) SPUtil.get(context, SPConstant.TAG_AVOID_CONGESTION, false) != avoidCongest) {
                listener.onDismiss();
            }
        });

        smartRecom.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                noHighSpeed.setChecked(false);
                avoidCharges.setChecked(false);
                avoidCongestion.setChecked(false);
            }
            SPUtil.put(context, SPConstant.TAG_SMART_RECOM, isChecked);
        });

        noHighSpeed.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) smartRecom.setChecked(false);
            SPUtil.put(context, SPConstant.TAG_NO_HIGH_SPEED, isChecked);
        });

        avoidCharges.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) smartRecom.setChecked(false);
            SPUtil.put(context, SPConstant.TAG_AVOID_CHARGES, isChecked);
        });

        avoidCongestion.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) smartRecom.setChecked(false);
            SPUtil.put(context, SPConstant.TAG_AVOID_CONGESTION, isChecked);
        });

    }


}
